package com.example.demo.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;

/**
 * @Package: com.zhang.config
 * @ClassName: GatewayConfiguration
 * @Author: Think
 * @CreateTime: 2021/7/23 14:39
 * @Description:
 */
@Component
public class GatewayConfiguration {

    /**
     * 设置限流或者降级之后的处理方法，对所有路径都生效
     */
    @PostConstruct
    public void doInit(){
        GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
            @Override
            public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
                return ServerResponse.status(200).syncBody("gateway提示：系统繁忙，请稍后再试！");
            }
        });
    }

}
